//
//  JHAssetsManager.m
//  JHAssetsPickerViewController
//
//  Created by admin on 15/11/16.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "JHAssetsManager.h"

@interface JHAssetsManager ()

@end

@implementation JHAssetsManager

+ (instancetype)defaultManager
{
    static JHAssetsManager *defaultManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        defaultManager = [[self alloc]init];
    });
    return defaultManager;
}
- (NSString *)localIdentifierWithAsset:(PHAsset *)asset type:(NSString *)type
{
    return [NSString stringWithFormat:@"%@_%@", type, asset.localIdentifier];
}
//缩略图
- (void)thumbnailWithAsset:(PHAsset *)asset size:(CGSize)size completion:(void (^)(UIImage *image))completion
{
    CGFloat scale = [UIScreen mainScreen].scale;
    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestImageForAsset:asset
                       targetSize:CGSizeMake(size.width*scale, size.height*scale)
                      contentMode:PHImageContentModeAspectFill
                          options:[JHAssetsManager thumbnailRequestOptions]
                    resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                        completion(result);
                    }];
}
//预览图
- (void)previewWithAsset:(PHAsset *)asset completion:(void (^)(UIImage *image))completion
{

    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestImageForAsset:asset
                       targetSize:[JHAssetsManager sizeOfPreview]
                      contentMode:PHImageContentModeAspectFill
                          options:[JHAssetsManager previewRequestOptions]
                    resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                        completion(result);
                    }];
}
+ (CGSize)sizeOfPreview
{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetHeight([UIScreen mainScreen].bounds) * scale);
    return  size;
//    return PHImageManagerMaximumSize;
}
+ (PHImageRequestOptions *)previewRequestOptions
{
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    //options.resizeMode = PHImageRequestOptionsResizeModeFast;
    options.networkAccessAllowed = NO;
    options.synchronous = NO;
    return options;
}
+ (PHImageRequestOptions *)thumbnailRequestOptions
{
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    options.networkAccessAllowed = NO;
    options.synchronous = NO;
    return options;
}

@end
